//
//  UIBarButtonItem+FLMExtension.m
//  FLMSuperS
//
//  Created by xiaoyang on 16/9/6.
//  Copyright © 2016年 FaLaoMiaoNetworkTechnologyCo. All rights reserved.
//

#import "UIBarButtonItem+FLMExtension.h"

@implementation UIBarButtonItem (FLMExtension)
/**
 *  创建一个item
 *
 *  @param target     点击item后调用哪个对象的方法
 *  @param action     点击item后调用target的哪个方法
 *  @param image      图片
 *  @param hightImage 高亮的图片
 *
 *  @return 创建完的item
 */

+ (UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action image:(NSString *)image hightImage:(NSString *)hightImage
{
    //自定义按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    //监听按钮点击
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    //设置图片
    [btn setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:hightImage] forState:UIControlStateHighlighted];
    
    //设置按钮尺寸为当前图片的size
    btn.size = btn.currentBackgroundImage.size;
    
    //返回按钮位置
    return [[UIBarButtonItem alloc] initWithCustomView:btn];
}

+ (UIBarButtonItem *)itemWithImage:(UIImage *)image highImage:(UIImage *)highImage target:(id)target action:(SEL)action
{
    //自定义按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    //设置按钮默认状态
    [btn setImage: image forState:UIControlStateNormal];
    //设置按钮高亮状态
    [btn setImage:highImage forState:UIControlStateHighlighted];
    //设置按钮自适应
    [btn sizeToFit];
    
    //监听按钮点击
    [btn addTarget:target action:(action) forControlEvents:UIControlEventTouchUpInside];
    
    //解决导航条点击按钮过大
    //包装uiview
    UIView *viewBtn = [[UIView alloc] initWithFrame:btn.bounds];
    [viewBtn addSubview:btn];
    
    //返回按钮位置
    return [[UIBarButtonItem alloc] initWithCustomView:viewBtn];
}

+ (UIBarButtonItem *)itemWithImage:(UIImage *)image selectImage:(UIImage *)selectImage target:(id)target action:(SEL)action
{
    //自定义按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    //设置按钮默认状态
    [btn setImage: image forState:UIControlStateNormal];
    //设置按钮高亮状态
    [btn setImage:selectImage forState:UIControlStateSelected];
    //设置按钮自适应
    [btn sizeToFit];
    
    //监听按钮点击
    [btn addTarget:target action:(action) forControlEvents:UIControlEventTouchUpInside];
    //包装uiview
    UIView *viewBtn = [[UIView alloc] initWithFrame:btn.bounds];
    [viewBtn addSubview:btn];
    
    //返回按钮位置
    return [[UIBarButtonItem alloc] initWithCustomView:viewBtn];
}

/**
 *  创建一个item标题
 *
 *  @param target     点击item后调用哪个对象的方法
 *  @param action     点击item后调用target的哪个方法
 *  @param title      标题
 *  @return 创建完的item
 */
+ (UIBarButtonItem *)itemWithTitle:(NSString *)title target:(id)target action:(SEL)action
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn sizeToFit];
    [btn addTarget:target action:(action) forControlEvents:UIControlEventTouchUpInside];
    UIView *viewBtn = [[UIView alloc] initWithFrame:btn.bounds];
    [viewBtn addSubview:btn];
    
    return [[UIBarButtonItem alloc] initWithCustomView:viewBtn];
}
@end
